package com.ic.service.impl;

import com.ic.base.AbstractBaseService;
import com.ic.base.PageInfo;
import com.ic.dao.SubjectDao;
import com.ic.entity.Subject;
import com.ic.service.SubjectService;
import com.ic.util.KeyGenerateUtil;
import com.ic.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 物品科目业务类
 * Author: spark
 * Date  : 2017/6/25
 * Time  : 17:31.
 */
@Service("subjectService")
public class SubjectServiceImpl extends AbstractBaseService<Subject> implements SubjectService {

	@Resource
	private SubjectDao subjectDao;

	/**
	 * 添加物品科目信息
	 *
	 * @param subject 物品科目
	 * @return 受影响的记录数量
	 */
	@Override
	public Integer insert(Subject subject) {
		subject.setId(KeyGenerateUtil.getInstance().getKey());
		return subjectDao.insert(subject);
	}

	/**
	 * 根据ID更新物品科目信息
	 *
	 * @param subject 物品科目
	 * @return 受影响的记录数量
	 */
	@Override
	public Integer updateByPrimaryKeySelective(Subject subject) {
		return subjectDao.updateByPrimaryKeySelective(subject);
	}

	/**
	 * 根据查询条件获取物品科目信息
	 *
	 * @param subject  物品科目
	 * @param pageInfo 分页信息
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map showSubjectList(Subject subject, PageInfo pageInfo) {
		Map queryMap = new HashMap();
		Map resultMap = new HashMap();
		List list = new ArrayList();
		queryMap.put("subjectName", subject.getSubjectName());
		// 查询符合条件的记录总数
		int totalSize = subjectDao.selectCount(queryMap);
		if (totalSize == 0) {
			resultMap.put("rows", 0);
			resultMap.put("total", list);
			return resultMap;
		}
		pageInfo.setTotalSize(totalSize);
		queryMap.put("startIndex", pageInfo.getStartIndex());
		queryMap.put("pageSize", pageInfo.getPAGE_SIZE());
		// 查询符合条件的记录信息
		list = subjectDao.showSubjectList(queryMap);
		resultMap.put("rows", list);
		resultMap.put("total", totalSize);
		return resultMap;
	}

	/**
	 * 检查物品科目名称是否已存在
	 *
	 * @param subject 物品科目
	 * @return 符合要求的物品科目名称数量
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map checkSubjectName(Subject subject) {
		Map queryMap = new HashMap();
		int count = subjectDao.checkSubjectName(subject);
		queryMap.put("count", count);
		return queryMap;
	}

	/**
	 * 添加物品科目信息
	 *
	 * @param subject 物品科目信息
	 */
	@Override
	public void addSubject(Subject subject) {
		insert(subject);
	}

	/**
	 * 根据物品科目ID更改物品科目状态
	 *
	 * @param ids   物品科目ID
	 * @param state 物品科目状态
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void updateState(String[] ids, String state) {
		Map map = new HashMap();
		List idList = StringUtil.generateList(ids);
		map.put("list", idList);
		map.put("state", state);
		subjectDao.updateState(map);
	}

	/**
	 * 根据物品科目ID查询物品科目信息
	 *
	 * @param id 物品科目ID
	 * @return 物品科目
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map showSubject(String id) {
		Map map = new HashMap();
		Subject subject = (Subject) subjectDao.showSubject(id).get(0);
		map.put("subject", subject);
		return map;
	}
}